package com.yserp.project.erp.basic.goods.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.basic.goods.domain.BasisGoods;
import com.yserp.project.erp.basic.goods.domain.BasisGoodsVo;
import com.yserp.project.erp.basic.goods.service.IBasisGoodsService;
import com.yserp.project.erp.basic.goodstype.domain.BasisGoodsType;
import com.yserp.project.erp.basic.goodstype.service.IBasisGoodsTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品信息Controller
 *
 * @author yserp
 * @date 2022-06-29
 */
@Controller
@RequestMapping("/basic/goods")
public class BasisGoodsController extends BaseController {
    private String prefix = "erp/basic/goods";

    @Autowired
    private IBasisGoodsService basisGoodsService;
    @Autowired
    private IBasisGoodsTypeService basisGoodsTypeService;

    @RequiresPermissions("basic:goods:view")
    @GetMapping()
    public String goods() {
        return prefix + "/goods";
    }

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("basic:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasisGoods basisGoods) {
        startPage();
        List<BasisGoodsVo> list = basisGoodsService.selectBasisGoodsListVo(basisGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("basic:goods:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasisGoods basisGoods) {
        List<BasisGoods> list = basisGoodsService.selectBasisGoodsList(basisGoods);
        ExcelUtil<BasisGoods> util = new ExcelUtil<BasisGoods>(BasisGoods.class);
        return util.exportExcel(list, "商品信息数据");
    }

    /**
     * 新增商品信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品信息
     */
    @RequiresPermissions("basic:goods:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasisGoods basisGoods) {
        return toAjax(basisGoodsService.save(basisGoods));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("basic:goods:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BasisGoods basisGoods = basisGoodsService.selectBasisGoodsById(id);
        BasisGoodsType goodsType = basisGoodsTypeService.selectBasisGoodsTypeById(basisGoods.getGoodsTypeId());
        mmap.put("basisGoods", basisGoods);
        mmap.put("goodsType", goodsType);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品信息
     */
    @RequiresPermissions("basic:goods:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasisGoods basisGoods) {
        return toAjax(basisGoodsService.updateById(basisGoods));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("basic:goods:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(basisGoodsService.deleteBasisGoodsByIds(ids));
    }


    /**
     * 校验商品编码
     */
    @PostMapping("/checkGoodsCodeUnique")
    @ResponseBody
    public String checkGoodsCodeUnique(BasisGoods basisGoods) {
        return basisGoodsService.checkGoodsCodeUnique(basisGoods.getGoodsCode());
    }

    /**
     * 选择商品信息
     */
    @GetMapping("/selectGoods")
    public String selectGoods() {
        return prefix + "/selectGoods";
    }

    /**
     * 查询商品信息列表(选择页面)
     */
    @PostMapping("/selectList")
    @ResponseBody
    public TableDataInfo selectList(BasisGoods basisGoods) {
        startPage();
        List<BasisGoodsVo> list = basisGoodsService.selectBasisGoodsListVo(basisGoods);
        return getDataTable(list);
    }
}
